package com.yuji.aacar.controller.self;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.Const;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.bean.response.LogOutResponse;
import com.yuji.aacar.bean.response.LoginResponse;
import com.yuji.aacar.controller.changepassword.ChangePasswordActivity;
import com.yuji.aacar.controller.others.EditContentActivity;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.utils.UmengUtils;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <li>Package:com.yuji.aacar.controller.self</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/6</li>
 * <li>Description:   </li>
 */

public class SelfInfoActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.self_info_nickname_tv)
    PingFangTextView selfInfoNicknameTv;
    @BindView(R.id.self_info_nickname_rlayout)
    AutoRelativeLayout selfInfoNicknameRlayout;
    @BindView(R.id.self_info_name_tv)
    PingFangTextView selfInfoNameTv;
    @BindView(R.id.self_info_name_rlayout)
    AutoRelativeLayout selfInfoNameRlayout;
    @BindView(R.id.self_info_phone_tv)
    PingFangTextView selfInfoPhoneTv;
    @BindView(R.id.self_info_phone_rlayout)
    AutoRelativeLayout selfInfoPhoneRlayout;
    @BindView(R.id.self_info_sex_tv)
    PingFangTextView selfInfoSexTv;
    @BindView(R.id.self_info_sex_rlayout)
    AutoRelativeLayout selfInfoSexRlayout;
    @BindView(R.id.self_info_birth_tv)
    PingFangTextView selfInfoBirthTv;
    @BindView(R.id.self_info_birth_rlayout)
    AutoRelativeLayout selfInfoBirthRlayout;
    @BindView(R.id.self_info_change_password_rlayout)
    AutoRelativeLayout selfInfoChangePasswordRlayout;
    @BindView(R.id.self_info_exit_btn)
    Button selfInfoExitBtn;
    private SelfInfoActivityVM selfInfoActivityVM;

    private LoginResponse user;

    private String nickName;
    private String userName;
    private String phone;
    private String sex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_self_info);
        ButterKnife.bind(this);
        initTitle(true, "个人信息", null);
        backLogin.setText("保存");
        backLogin.setVisibility(View.VISIBLE);
        backLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestSaveInfo();
            }
        });
        super.onCreate(savedInstanceState);
        initUI();
    }

    private void initUI() {
        user = UserCenter.getCurrentUser();
        if (null != user) {
            nickName = user.getNickName();
            userName = user.getUserName();
            phone = user.getPhoneNo();
            sex = user.getSex();
            selfInfoPhoneTv.setTextColor(getResources().getColor(R.color.hintcolor));
            selfInfoNicknameTv.setText(nickName);
            selfInfoNameTv.setText(userName);
            selfInfoPhoneTv.setText(phone);
            selfInfoSexTv.setText(sex);
        }
    }

    @OnClick({R.id.self_info_nickname_rlayout, R.id.self_info_name_rlayout, R.id.self_info_sex_rlayout, R.id.back_login, R.id.self_info_change_password_rlayout, R.id.self_info_exit_btn})
    public void onClick(View view) {
        Intent intent = getIntent();
        switch (view.getId()) {
            case R.id.back_login:
                break;
            case R.id.self_info_change_password_rlayout:
                presentController(ChangePasswordActivity.class);
                break;
            case R.id.self_info_exit_btn:
                DialogInterface.OnClickListener a = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        requestParams = BaseServiceParams.loginOut();
                        doTask(BaseServiceMediator.USER_LOGIN_OUT);

                    }
                };
                showAlertDialogue("取消", "确定退出登录？", "确定", a);
                break;
            case R.id.self_info_nickname_rlayout:
                nickName = selfInfoNicknameTv.getText().toString();
                intent.putExtra("content", nickName);
                intent.putExtra("type", "nickName");
                intent.putExtra("from", Const.FROM_PERSONAL_INFO);
                presentController(EditContentActivity.class, intent);
                break;
            case R.id.self_info_name_rlayout:
                userName = selfInfoNameTv.getText().toString();
                intent.putExtra("content", userName);
                intent.putExtra("type", "userName");
                intent.putExtra("from", Const.FROM_PERSONAL_INFO);
                presentController(EditContentActivity.class, intent);

                break;
            case R.id.self_info_sex_rlayout:
                sex = selfInfoSexTv.getText().toString();
                intent.putExtra("content", sex);
                intent.putExtra("type", "sex");
                intent.putExtra("from", Const.FROM_PERSONAL_INFO);
                presentController(EditContentActivity.class, intent);
                break;
        }
    }

    private void requestSaveInfo(){
        requestParams = BaseServiceParams.update_member(nickName , userName, sex);
        doTask(BaseServiceMediator.UPDATE_MEMBER);
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.USER_LOGIN_OUT_CODE) {
            LogOutResponse logOutResponse = (LogOutResponse) response.getResponse();
            if (logOutResponse.isSuccess()) {
                UmengUtils.onLogout();
                UserCenter.logout(SelfInfoActivity.this);
                CoreEventCenter.postMessage(EventConst.LOGIN_OUT, "退出登录");
//                presentController(SplashActivity.class);
                this.finish();
            }
        } else if (response.getRequestCode() == BaseServiceMediator.UPDATE_MEMBER_CODE) {
            String result = (String)response.getResponse();
            if (result.contentEquals("1")){
                user = UserCenter.getCurrentUser();
                if (null != user){
                    user.setNickName(nickName);
                    user.setUserName(userName);
                    user.setSex(sex);
                    UserCenter.saveCurrentUser(user);

                    CoreEventCenter.postMessage(EventConst.PERSONAL_NAME_CHANGED,userName);
                }
                showToast("保存成功");
            }
        }
    }

    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);

        if (message.getMessageCode().contentEquals(String.valueOf(EventConst.PERSONAL_NICKNAME))) {
            nickName = message.getMessage();
            selfInfoNicknameTv.setText(nickName);
        } else if (message.getMessageCode().contentEquals(String.valueOf(EventConst.PERSONAL_NAME))) {
            userName = message.getMessage();
            selfInfoNameTv.setText(userName);
        } else if (message.getMessageCode().contentEquals(String.valueOf(EventConst.PERSONAL_SEX))) {
            sex = message.getMessage();
            selfInfoSexTv.setText(sex);
        }
    }
}
